const app = getApp()

Page({
  data: {
    decoding_api: 'http://192.168.20.31:8084/scm-wechat/weChatAction/weChatBind.do',
    session_keyApi: 'https://api.weixin.qq.com/sns/jscode2session',
    user_session_key:"",
    user_openid:""
    
  },
  onLoad: function () {
    
    new Promise((resolve, reject) => {
      //获取用户 code
      console.log("获取用户code页面")
      wx.login({
        success: (res) => {
          resolve(res);
        }
      })
    }).then((res) => {
      return new Promise((resolve, reject) => {
        //获取用户openId 与  session_key
        
        wx.request({
          url: this.data.session_keyApi,
          data: {
            appid: 'wxebb09397323ee44b',        // 设置微信的appid
            secret: '7dbb6bf381fde4796796ea0d23fdf204',
            js_code: res.code,
            grant_type: 'authorization_code'
          },
          success: (res) => {
            app.globalData.userInfo = {oppenId: res.data.openid}
            let data = {
              user_openid: res.data.openid
            }
            //因为resolve 只能接收一个参数所以将该数据以对象的方式传递方便下一步
            console.log("data---"+res.data.openid)
            resolve(data);
          }
        })
      })
    })
    // .then((data) => {
    //   return new Promise((resolve, reject) => {
    //     //获取用户公开信息与敏感信息
    //     //如果只要用户的openId上一步就已经获取到了
    //     //此步是为了获取用户公开信息如：头像、昵称、所在城市等·····
    //     wx.getUserInfo({
    //       success: (res) => {
    //         let da = {
    //           encryptedData: res.encryptedData,
    //           iv: res.iv
    //         };
    //         Object.assign(da, data);
    //         resolve(da);
    //       },
    //       fail: () => {
    //         reject();
    //       }
    //     })
    //   })
    // })
    .then((data) => {
      //将获取到的session_key encryptedData iv 等传到服务器进行解密
      wx.request({
        url: this.data.decoding_api,
        method: 'GET',
        dataType: 'json',
        data: {
          "openID":"gh_d17c225d5ec3",
          "userOpenID":data.user_openid,
          "username":"ysfw",
          "password":'123'
        },
        success: (res) => {
          console.log(res);
          if(res.data.success){
            wx.redirectTo({
              url: '/pages/invoice/main'
            })
          }else{
            wx.redirectTo({
              url: '/pages/login/login'
            })
          }
          
        },
        fail: (res) =>{
          console.log(res);
          wx.redirectTo({
            url: '/pages/login/login'
          })
       }
       
      })
    }).catch(() => {
      //接收用户拒绝授权抛出的错误
      //弹出提示框提示用户
      console.log("request is fail -------")
      wx.showModal({
        title: '登录失败',
        content: '您需要授权才能使用',
        confirmText: '去授权',
        cancelText: '拒绝授权',
        success: (res) => {
          if (res.confirm == true) {
            //跳转到设置页面重新打开权限
            wx.openSetting({
              success: (res) => {
                if (res.authSetting['scope.userInfo'] == true) {
                  //重新获取用户公开信息
                  wx.getUserInfo({
                    success: (res) => {
                      //进行解密
                      wx.request({
                        url: this.data.decoding_api,
                        method: 'GET',
                        dataType: 'json',
                        data: {
                          appid: this.data.appId,
                          session_key: this.data.user_session_key,
                          encryptedData: res.encryptedData,
                          iv: res.iv
                        },
                        header:{

                        },
                        success: (res) => {
                          this.setData({
                           
                            user_openid: res.data.openId
                          });
                        }
                      })
                    }
                  })
                } else if (res.authSetting['scope.userInfo'] == false) {
                  //用户拒绝重新开启权限跳转到提示页
                  wx.redirectTo({
                    url: '/pages/login/login'
                  })
                }
              }
            })
          } else if (res.confirm == false) {
            //用户拒绝重新开启权限跳转到提示页
            wx.redirectTo({
              url: '/pages/login/login'
            })
          }
        }
      })
    })
  }
})
